from direct.gui.DirectGui import DirectEntry
from QScrolledFrame import QScrolledFrame
from Cube.gui.DirectGUIPropertiesAccess import HeightGet as FrameHeightGet
from Cube.gui.DirectGUIPropertiesAccess import WidthGet as FrameWidthGet
from Cube.gui.DirectGUIPropertiesAccess import AlignText
from pandac.PandaModules import TextNode, PGEntry


DefaltParameter_Entry = dict(frameSize = (-1.2, 1.2, -0.05, 0.05),
                             text_scale = 0.07,
                             width = 100,  # how to control word cnt?
                             backgroundFocus = 1,
                             initialText = ''
                             )

class QScrolledEntryFrame(QScrolledFrame):
    def __init__(self,
                 parent = None,
                 param_Entry = {},
                 param_ScrolledFrame = {}):

        paramIn_Entry = DefaltParameter_Entry.copy()
        paramIn_Entry.update(param_Entry)
        if not paramIn_Entry.has_key("command"): paramIn_Entry["command"] = self.MessageEnter
        textEntry = DirectEntry(**paramIn_Entry)

        QScrolledFrame.__init__(self,
                                parent = parent,
                                packObject = textEntry,
                                fixVertical = True,
                                param_ScrolledFrame = param_ScrolledFrame
                                )

        #AlignText(textEntry, "left")
        self.textObject = textEntry.component( textEntry.components()[0] )

        self.textEntry = textEntry
        self.enterText = textEntry.enterText

        self.PropertiesSet()

        #textEntry.accept(PGEntry.getTypeEvent(textEntry.guiItem), self.TypeCallBack)

    def EntryCallBackSet(self, fn):
        self.textEntry["command"] = fn

    def MessageEnter(self,text):
        print text

    def PropertiesSet(self):
        # QScrolledFrameHeightSet
        textHScale = self.textObject['scale'][1]
        textEntryHeight = FrameHeightGet(self.textEntry)
        scrollBarWidth = self._QScrolledFrame["scrollBarWidth"]
        frameHeight = textEntryHeight + (textHScale*0.5) + scrollBarWidth
        self._QScrolledFrame["frameSize"] = (0,0, 0, frameHeight)

        #CanvasSizeSet
        half_textEntryHeight = textEntryHeight*0.5
        textEntryWidthGet = FrameWidthGet(self.textEntry)
        self._QScrolledFrame['canvasSize'] = (0,textEntryWidthGet, 0, textEntryHeight - (textHScale*0.25))

    def TypeCallBack(self, text):
        print text

    def EntryClear(self):
        self.textEntry.enterText('')
